//index.js
//获取应用实例
var app = getApp()
Page({
    data: {
        scrollTop: {
            scroll_top: 0,
            goTop_show: false
        },
        isshow_subsidy: true,
        scrollHeight: 0,
        pageheight: 1200,
        currentTab: 0,
        indicatorDots: true,
        autoplay: false,
        interval: 3000,
        duration: 100,
        page: 1,
        sort: 0,
        cname: "",
        cname_show: "",
        keyword: "",
        pagesize: 20,
        uri: "list",
        from_uid: 0,
        memberid: 0,
        globalset: {},
        datalock: false,
        commission: 0,
        imgmenu: [
        ],
        imgmenu2: [
        ],
        commodity: [],
    },

    onLoad: function (options) {
     
        var $this = this;

        wx.getSystemInfo({
            success: function (res) {
                $this.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });
    },
    onReady: function () {
     
        var $this = this;

        var cname = wx.getStorageSync("cname");
        if (cname != "") {
            $this.setData({
                cname: cname,
                uri: "listbycname"
            });
        }
        var cname_show = wx.getStorageSync("cname_show");
        if (cname_show != "") {
            $this.setData({
                cname_show: cname_show
            });
        }
        /*
        var searchkey = wx.getStorageSync("searchkey");
        if (searchkey != "") {
            $this.setData({
                keyword: searchkey,
                cname: "",
                uri: "listbysearch"
            });
        }*/


        //取会员对象的方式
        var memberinfo = wx.getStorageSync('memberinfo');

        $this.setData({
            memberinfo:memberinfo,
            from_uid: memberinfo.memberid,
            memberid: memberinfo.memberid,
        });
      
        var globalset = wx.getStorageSync('globalset');
        //取会员对象的方式
        $this.setData({
            globalset: globalset
        });
        //图标菜单
        var head_menu_str = globalset.head_menu;
        var head_menu_json = JSON.parse(head_menu_str);
        if (typeof head_menu_json.list != undefined && head_menu_json.list != null && head_menu_json.list != '') {

            var menulen = head_menu_json.list.length;
            var newhead_menu_json = [];
            var menulen = head_menu_json.list.length;
            for (var i = 0; i < 8; i++) {
                newhead_menu_json.push(head_menu_json.list[i])
            }


            $this.setData({
                imgmenu: newhead_menu_json,
                datalock: false

            });
            var imgmenu2 = new Array();
            for (var i = 8; i < menulen; i++) {
                imgmenu2.push(head_menu_json.list[i]);
            }
            $this.setData({
                imgmenu2: imgmenu2,
                datalock: false
            });
            var imgmenu22 = new Array();
            for (var i = 16; i < menulen; i++) {
                imgmenu22.push(head_menu_json.list[i]);
            }
            $this.setData({
                imgmenu22: imgmenu22,
                datalock: false
            });
            var imgmenu222 = new Array();
            for (var i = 24; i < menulen; i++) {
                imgmenu222.push(head_menu_json.list[i]);
            }
            $this.setData({
                imgmenu222: imgmenu222,
                datalock: false
            });
        }
        //计算总的佣金比例
        for (var i = 0; i < globalset.memberlevel.length; i++) {
           
            //当前会员类型的等级佣金比例
            if (globalset.memberlevel[i].type == memberinfo.type && globalset.memberlevel[i].name == memberinfo.level) {
                var total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2) + parseFloat(globalset.memberlevel[i].myleader1_credit2) + parseFloat(globalset.memberlevel[i].myleader2_credit2);
                //换成只取1级佣金的比例
                var total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2);
              
                $this.setData({
                    commission: total_commission,
                    datalock: false
                });
              
            }
        }
        if (memberinfo.type == 0) {
            if (globalset.isopen_subsidy == 0) {
                $this.setData({
                    isshow_subsidy: false
                });
            }
           
            var total_commission = parseFloat(globalset.subsidy);
            $this.setData({
                commission: total_commission,
                datalock: false
            });
        }

        if (globalset.isshow_subsidy == 0) {
            $this.setData({
                isshow_subsidy: false
            });
        }


        
        $this.goodList(1);

    },

    onReachBottom: function () {
      
        var $this = this;
        var page = $this.data.page + 1;
        var second_height = $this.data.second_height * page;
        $this.setData({ second_height: second_height });
        $this.goodList(page);
    },
    //事件处理函数
    skiptodetail: function (e) {
        var skuId = e.currentTarget.dataset.skuid;
        for (var i = 0; i < this.data.commodity.length; i++) {
            if (this.data.commodity[i].skuId == skuId) {
                var good = this.data.commodity[i];
                wx.setStorage({
                    key: "good",
                    data: good
                });
            }
        }

        wx.navigateTo({
            url: '../detail/index?skuId=' + skuId
        })
    },
    bindChange: function (e) {

        var that = this;
        that.setData({ currentTab: e.detail.current });

    },

    swichNav: function (e) {

        var that = this;

        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current
            })
        }
    },
    goodList: function (page, callback) {
        var $this = this;
        if ($this.data.datalock) {
            return false;
        }
        $this.setData({
            page: page,
            datalock: true,
        });
       
        app.util.request({
            url: 'entry/wxapp/Goods',
            data: {
                m: 'nets_haojk',
                page: page,
                uri: $this.data.uri,
                sort: 7,
               // keyword: $this.data.keyword,
                //cname: $this.data.cname
            },
            method: 'post',
            success: function (response) {
                if (typeof callback == 'function') {
                    callback(response.data.data);
                    $this.setData({
                        datalock: false
                    });
                } else {
                    var goods = $this.data.commodity;
                    if (response.data.data) {
                        for (var i in response.data.data) {
                            if ($this.data.commission == 0) {
                                response.data.data[i].wlPrice_after1 = response.data.data[i].discount;
                            } else {
                                response.data.data[i].wlPrice_after1 = response.data.data[i].wlPrice_after * (response.data.data[i].wlCommissionShare / 100) * ($this.data.commission / 100)*0.9;
                                if (response.data.data[i].wlPrice_after1 > 0)
                                    response.data.data[i].wlPrice_after1 = response.data.data[i].wlPrice_after1.toFixed(2);
                               


                            }
                            goods.push(response.data.data[i]);
                        }
                    }
                    $this.setData({
                        commodity: goods,
                        datalock: false
                    });
                }
            }
        });
    },
    bysort: function (e) {
        var $this = this;

        wx.getSystemInfo({
            success: function (res) {
                $this.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });
        this.setData({
            sort: e.currentTarget.dataset.sort
        });
        this.setData({
            commodity: [],
            datalock: false
        });
        this.goodList(1);
    },
    bycname: function (e) {
        var $this = this;

        wx.getSystemInfo({
            success: function (res) {
                $this.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });
      
        var cname = e.currentTarget.dataset.url.split("=")[1];
        this.setData({
            cname_show: e.currentTarget.dataset.cname
        });

        this.setData({
            cname: cname,
            uri: "listbycname"
        });
        this.setData({
            commodity: [],
            datalock: false
        });
        this.setData({
            keyword: ""
        });
        wx.setStorage({
            key: "cname",
            data: e.currentTarget.dataset.cname
        });
        wx.setStorage({
            key: "searchkey",
            data: ""
        });
        this.goodList(1);
    },
    searchkey: function (e) {
        wx.setStorageSync("searchkey", this.data.searchkey);
        wx.setStorageSync('limittime', '');
        wx.setStorageSync('realtime', '');
        if (this.data.keyword != "") {
            wx.navigateTo({
                url: "../search/index",

            })
        } else {
            this.setData({
                commodity: [],
                datalock: false,
                keyword: "",
                cname: "",
                cname_show: "",
            });

            wx.setStorage({
                key: "cname",
                data: ""
            });
           
            this.goodList(1);
        }





    },
    bindKeyInput: function (e) {
        var value = e.detail.value;
        var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
        var objExp = new RegExp(Expression);
        if (objExp.test(value) == true) {
            //是URL
            if (value.indexOf(".html") > 0) {
                value = value.split('?')[0];
                var valuearr = value.split("/");
                value = valuearr[valuearr.length - 1];
                value = value.replace(".html", "");
            }
            if (value.indexOf("sku=") > 0) {
                value = value.split('sku=')[1];
                var valuearr = value.split("&");
                value = valuearr[0];
            }
        }
       
        this.setData({
            searchkey: value
        });
        wx.setStorage({
            key: "searchkey",
            data: value
        });

     

        this.setData({
            keyword: value,
            cname: "",
            uri: "listbysearch"
        });
        wx.setStorage({
            key: "cname",
            data: ""
        });
    },
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
        }
        return {
            title: this.data.globalset.title,
            path: '/haojk/pages/index/index?from_uid=' + this.data.memberid,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },
    scrollTopFun: function (e) {
     
        this.setData({
            'pageheight': e.detail.scrollHeight
        });
        if (e.detail.scrollTop > 300) {//触发gotop的显示条件 
            this.setData({
                'scrollTop.goTop_show': true
            });
        } else {
            this.setData({
                'scrollTop.goTop_show': false
            });
        }
    },
    goTopFun: function (e) {
        var _top = this.data.scrollTop.scroll_top;//发现设置scroll-top值不能和上一次的值一样，否则无效，所以这里加了个判断 
        if (_top == 0) {
            _top = 1;
        } else {
            _top = 0;
        }
        this.setData({
            'scrollTop.scroll_top': _top
        });
    },
    //返回上一页
    backPrev: function () {
        wx.navigateBack({
            delta: 1
        })
    },
})
